package com.beardedhen.harmonybootstrap;

import com.beardedhen.harmonybootstrap.api.defaults.DefaultBootstrapBrand;
import com.beardedhen.harmonybootstrap.api.defaults.DefaultBootstrapSize;
import com.beardedhen.harmonybootstrap.api.view.RoundableView;
import com.beardedhen.harmonybootstrap.utils.ResUtil;
import com.beardedhen.harmonybootstrap.utils.UiUtil;
import com.beardedhen.harmonybootstrap.utils.ViewUtils;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.element.Element;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;

import org.jetbrains.annotations.Nullable;

/**
 * BootstrapThumbnail displays a rectangular image with an optional border, that can be
 * themed. The view extends Image, and will automatically center crop and
 * scale images.
 */
public class BootstrapThumbnail extends BootstrapBaseThumbnail implements RoundableView, Component.DrawTask {

    private static final String BOOTSTRAP_THUMBNAIL_BOOTSTRAP_BRAND = "bootstrapBrand";

    private static final String BOOTSTRAP_THUMBNAIL_BOOTSTRAP_SIZE = "bootstrapSize";

    private static final String BOOTSTRAP_THUMBNAIL_HAS_BORDER = "hasBorder";

    private static final String BOOTSTRAP_THUMBNAIL_ROUNDED_CORNERS = "roundedCorners";

    private Paint placeholderPaint;

    private final RectFloat imageRect = new RectFloat();

    private boolean roundedCorners;

    private float baselineCornerRadius;

    public BootstrapThumbnail(Context context) {
        super(context);
        initialise(null);
    }

    public BootstrapThumbnail(Context context, AttrSet attrs) {
        super(context, attrs);
        initialise(attrs);
    }

    public BootstrapThumbnail(Context context, AttrSet attrs, String defStyle) {
        super(context, attrs, defStyle);
        initialise(attrs);
    }

    @Override
    protected void initialise(@Nullable AttrSet attrs) {

        if (attrs == null) {
            return;
        }

        int typeOrdinal = -1;
        if (attrs.getAttr(BOOTSTRAP_THUMBNAIL_BOOTSTRAP_BRAND).isPresent()) {
            String typeOrdinalStr = attrs.getAttr(BOOTSTRAP_THUMBNAIL_BOOTSTRAP_BRAND).get().getStringValue().trim();
            typeOrdinal = UiUtil.getBootstrapBrandInt(typeOrdinalStr);
        }

        int sizeOrdinal = -1;
        if (attrs.getAttr(BOOTSTRAP_THUMBNAIL_BOOTSTRAP_SIZE).isPresent()) {
            String sizeOrdinalStr = attrs.getAttr(BOOTSTRAP_THUMBNAIL_BOOTSTRAP_SIZE).get().getStringValue().trim();
            sizeOrdinal = UiUtil.getBootstrapSizeInt(sizeOrdinalStr);
        }

        hasBorder = !attrs.getAttr(BOOTSTRAP_THUMBNAIL_HAS_BORDER).isPresent() || attrs.getAttr(
            BOOTSTRAP_THUMBNAIL_HAS_BORDER).get().getBoolValue();

        bootstrapSize = DefaultBootstrapSize.fromAttributeValue(sizeOrdinal).scaleFactor();
        if (typeOrdinal == -1) { // override to use Primary for default border (looks nicer)
            bootstrapBrand = DefaultBootstrapBrand.PRIMARY;
        } else {
            bootstrapBrand = DefaultBootstrapBrand.fromAttributeValue(typeOrdinal);
        }

        if (attrs.getAttr(BOOTSTRAP_THUMBNAIL_ROUNDED_CORNERS).isPresent()) {
            roundedCorners = attrs.getAttr(BOOTSTRAP_THUMBNAIL_ROUNDED_CORNERS).get().getBoolValue();
        }

        placeholderPaint = new Paint();
        placeholderPaint.setColor(new Color(ResUtil.getColor(getContext(), ResourceTable.Color_bootstrap_gray_light)));
        placeholderPaint.setStyle(Paint.Style.FILL_STYLE);
        placeholderPaint.setAntiAlias(true);

        baselineCornerRadius = ResUtil.getDimen(getContext(), ResourceTable.Float_bthumbnail_rounded_corner);
        baselineBorderWidth = ResUtil.getDimen(getContext(), ResourceTable.Float_bthumbnail_default_border);

        setScaleMode(ScaleMode.CLIP_CENTER);
        addDrawTask(this);
        super.initialise(attrs);
    }

    @Override
    protected void updateImageState() {
        updateBackground();
        updatePadding();
        invalidate();
    }

    private void updateBackground() {
        Element bg = null;

        if (hasBorder) {
            bg = BootstrapDrawableFactory.bootstrapThumbnail(getContext(), bootstrapBrand,
                (int) (baselineOuterBorderWidth * bootstrapSize),
                ResUtil.getColor(getContext(), ResourceTable.Color_bootstrap_thumbnail_background), roundedCorners);
        }
        ViewUtils.setBackgroundDrawable(this, bg);
    }

    private void updatePadding() {
        int p = hasBorder ? (int) (baselineBorderWidth * bootstrapSize) : 0;
        setPadding(p, p, p, p);
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        if (sourceBitmap == null) { // draw a placeholder
            float padding = hasBorder ? (baselineBorderWidth * bootstrapSize) : 0;
            imageRect.top = padding;
            imageRect.bottom = getHeight() - padding;
            imageRect.left = padding;
            imageRect.right = getWidth() - padding;

            if (roundedCorners) {
                canvas.drawRoundRect(imageRect, (baselineCornerRadius * bootstrapSize),
                    (baselineCornerRadius * bootstrapSize), placeholderPaint);
            } else {
                canvas.drawRect(imageRect, placeholderPaint);
            }
        }
    }

    /*
     * Getters/setters
     */
    @Override
    public void setRounded(boolean rounded) {
        roundedCorners = rounded;
        updateImageState();
    }

    @Override
    public boolean isRounded() {
        return roundedCorners;
    }
}
